package com.yanqu.road.entity.enums;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * @author duli
 * @version 1.0
 * @date 2020/8/31 17:47
 * @Description
 */
public enum eErrorCode {
    /**
     * 基础错误
     */
    SUCCESS(0, "成功"),
    CALL_BACK_ERROR(103, "返回参数错误"),
    SYSTEM_ERROR(500, "系统错误"),

    ;
    private int code;

    private String message;

    eErrorCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static String getMessage(int code) {
        for (eErrorCode errorCode : eErrorCode.values()) {
            if (errorCode.getCode() == code) {
                return errorCode.getMessage();
            }
        }
        return "无错误信息";
    }

    public String toJson() {
        return toJson(null);
    }

    public String toJson(String message) {
        if (StringUtils.isBlank(message)) {
            message = this.message;
        }
        return "{\n\t\"code\": " + code + ",\n\t\"message\": \"" + message + "\"\n}";
    }

    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", this.getCode());
        jsonObject.put("message", this.getMessage());
        return jsonObject;
    }

}
